import {
    request,
    AbstractBookCrawler,
    BasicBook,
    SearchBookResult,
    Book,
    BasicChapter,
    BookSource,
    BookCrawlerTest,
    removeQuerySelector,
} from "../../common/common_node.js";

class BookCrawler extends AbstractBookCrawler {

    async searchBook(keyword, pageNum) {
        function getBoolList($) {
            const bookArr = [];
            const bookBoxArr = $(".side_commend ul li");
            bookBoxArr.each(function (i, e) {
                let infos = $(e).find("div:eq(1)").children();
                let nameAndPath = infos.eq(0);
                bookArr.push(new BasicBook({
                    name: nameAndPath.text(),
                    author: removeQuerySelector(infos.eq(1), "span").text().trim(),
                    path: nameAndPath.attr("href"),
                }));
            })
            return bookArr;
        }

        let url = `${this.source.searchBaseUrl}/search?searchkey=${encodeURIComponent(keyword)}&searchtype=all`;
        const doc = await (await request(url)).doc();
        const bookArr = getBoolList(doc);

        return new SearchBookResult({
            pageNum: 1,
            pages: 1,
            keyword: keyword,
            bookList: bookArr
        });
    }

    async bookDetails(path) {
        const $ = await (await request(this.source.bookBaseUrl + path)).doc();
        return new Book({
            name: $("meta[property='og:title']").attr('content'),
            author: $("meta[property='og:novel:author']").attr('content'),
            path: path,
            intro: $(".intro p:eq(1)").text(),
            coverUrl: $(".novel_info_main img").attr('src'),
            type: $("meta[property='og:novel:category']").attr('content'),
        });
    }

    async bookCatalog(path) {
        const $ = await (await request(this.source.bookBaseUrl + path)).doc();
        const chapters = $('#ul_all_chapters li a');
        let catalog = [];
        chapters.each(function (i, e) {
            const item = $(e);
            catalog.push(new BasicChapter({
                index: i,
                title: item.html(),
                path: item.attr("href")
            }));
        });
        return catalog;
    }

    async chapterContent(path) {
        const $ = await (await request(this.source.bookBaseUrl + path)).doc();
        let content = '';
        const pTags = $("#article p");
        pTags.each(function (index, e) {
            content += $(e).text();
            // 如果不是最后一个p标签，则添加换行符
            if (index !== pTags.length - 1) {
                content += '\n';
            }
        });
        return content;
    }
}

const source = {
    "id": "free_book_js/4",
    "name": "天籁小说网",
    "portalUrl": "https://www.tianlai01.com",
    "searchBaseUrl": "https://www.tianlai01.com",
    "bookBaseUrl": "https://www.tianlai01.com",
    "active": true
};

await new BookCrawlerTest(new BookCrawler(BookSource.createTestBookSource(source)), {
    enableSearch: true,
    keyword: "重生",
}).run();
